/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.restaurant;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.restaurant.FoodCategoryDao;
import com.boxin.ims.modules.wechat.entity.restaurant.FoodCategory;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 食品,菜品Service
 * @author Jakemanse
 * @version 2013-11-14
 */
@Component
@Transactional(readOnly = true)
public class FoodCategoryService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(FoodCategoryService.class);
	
	@Autowired
	private FoodCategoryDao foodCategoryDao;
	
	public FoodCategory get(Long id) {
		return foodCategoryDao.findOne(id);
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-14  下午5:42:27
	 * @function <p>根据用户ID查找所有类型  </p>
	 * @param userId
	 * @return
	 */
	public List<FoodCategory> findByUserId(Long userId){
		DetachedCriteria dc = foodCategoryDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("delFlag", FoodCategory.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("sort"));
		return foodCategoryDao.find( dc);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-22  下午10:02:48
	 * @function <p> 根据 餐厅ID查 菜品种类 </p>
	 * @param userId
	 * @return
	 */
	public List<FoodCategory> findByRestauranttId(Long userId){
		DetachedCriteria dc = foodCategoryDao.createDetachedCriteria();
		dc.add(Restrictions.eq("restaurant.id", userId));
		dc.add(Restrictions.eq("delFlag", FoodCategory.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("sort"));
		return foodCategoryDao.find( dc);
	}
	
	
	public Page<FoodCategory> find(Page<FoodCategory> page, FoodCategory foodCategory) {
		DetachedCriteria dc = foodCategoryDao.createDetachedCriteria();
		if (foodCategory.getUser()!=null && foodCategory.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", foodCategory.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(foodCategory.getName())){
			dc.add(Restrictions.like("name", "%"+foodCategory.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(foodCategory.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+foodCategory.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", FoodCategory.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return foodCategoryDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(FoodCategory foodCategory) {
		foodCategoryDao.save(foodCategory);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		foodCategoryDao.deleteById(id);
	}
	
}
